var tokenKey = "access-token";
var serverUrl = "http://localhost:8082";
// 例外不用token的地址
var exceptionAddrArr = ['/api/user/login', ];

//请求头处理函数
function CreateHeader(url, type) {
  let header = {}
  if (type == 'POST_PARAMS') {
    header = {
      'content-type': 'application/x-www-form-urlencoded'
    }
  } else {
    header = {
      'content-type': 'application/json'
    }
  }
  if (exceptionAddrArr.indexOf(url) == -1) { //排除请求的地址不须要token的地址
    let token = wx.getStorageSync(tokenKey);
    header['authorization'] = token;
  }
  header['appType'] = 'picApp';
  return header;
}

function uploadFile(url, data, filePath) {
  const deviceInfo = wx.getDeviceInfo()
  data['deviceInfo'] = deviceInfo.brand + '#' + deviceInfo.model + '#' + deviceInfo.platform
  data['platform'] = deviceInfo.platform
  let header = CreateHeader(url, 'POST');
  console.log(serverUrl + url);
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: serverUrl + url, //仅为示例，非真实的接口地址
      filePath: filePath,
      header:header,
      name: 'multipartFile',
      formData: data,
      success(res) {
      var response=  JSON.parse(res.data);
      console.log("成功",response)
        if (res.statusCode === 200 && response.code == 0) {
          //200: 服务端业务处理正常结束
          //存储token 到本地
          resolve(response)
        } else if (res.statusCode === 200 && response.code == 401) {
          wx.clearStorageSync();
          wx.showToast({
            title: response.msg,
            icon: 'none'
          })
          app.loginWx();
          reject(res.data)
        } else {
          wx.showToast({
            title: response.msg,
          })
          reject(response)
        }
      },
      fail: (res => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
        reject(res)
      })
    })
  })
}



//post请求，数据在body中
function postRequest(url, data) {
  const deviceInfo = wx.getDeviceInfo()
  data['deviceInfo'] = deviceInfo.brand + '#' + deviceInfo.model + '#' + deviceInfo.platform
  data['platform'] = deviceInfo.platform
  let header = CreateHeader(url, 'POST');
  console.log(serverUrl + url);
  return new Promise((resolve, reject) => {
    wx.request({
      url: serverUrl + url,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        console.log("成功", res.data)
        if (res.statusCode === 200 && res.data.code == 0) {
          //200: 服务端业务处理正常结束
          //存储token 到本地
          if (url == '/api/user/weChatLogin') {
            wx.setStorageSync(tokenKey, res.data.data)
          }
          resolve(res.data)
        } else if (res.statusCode === 200 && res.data.code == 401) {
          wx.clearStorageSync();
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          app.loginWx();
          reject(res.data)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }),
      fail: (res => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
        reject(res)
      })
    })
  })
}
//post请求，数据按照query方式传给后端
function postParamsRequest(url, data) {
  const deviceInfo = wx.getDeviceInfo()
  data['deviceInfo'] = deviceInfo.brand + '#' + deviceInfo.model + '#' + deviceInfo.platform
  data['platform'] = deviceInfo.platform
  let header = CreateHeader(url, 'POST_PARAMS');
  console.log(serverUrl + url);
  return new Promise((resolve, reject) => {
    wx.request({
      url: serverUrl + url,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        console.log("成功", res.data)
        if (res.statusCode === 200 && res.data.code == 0) {
          //200: 服务端业务处理正常结束
          //存储token 到本地
          if (url == '/api/user/weChatLogin') {
            wx.setStorageSync(tokenKey, res.data.data)
          }
          resolve(res.data)
        } else if (res.statusCode === 200 && res.data.code == 401) {
          wx.clearStorageSync();
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          app.loginWx();
          reject(res.data)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          reject(res.data)
        }
      }),
      fail: (res => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
        reject(res)
      })
    })
  })
}
//get 请求
function getRequest(url, data) {
  
  const deviceInfo = wx.getDeviceInfo()
  data['version'] =  getApp().globalData.version
  data['deviceInfo'] = deviceInfo.brand + '#' + deviceInfo.model + '#' + deviceInfo.platform
  data['platform'] = deviceInfo.platform
  let header = CreateHeader(url, 'GET');
  console.log(serverUrl + url);
  return new Promise((resolve, reject) => {
    wx.request({
      url: serverUrl + url,
      data: data,
      header: header,
      method: 'GET',
      success: (res => {
        console.log("成功", res.data)
        if (res.statusCode === 200 && res.data.code == 0) {
          //200: 服务端业务处理正常结束
          resolve(res.data)
        } else if (res.statusCode === 200 && res.data.code == 401) {
          wx.clearStorageSync();
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          app.loginWx();
          reject(res.data)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          reject(res.data)
        }
      }),
      fail: (res => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
        reject(res)
      })
    })
  })
}
//put请求
function putRequest(url, data) {
  let header = CreateHeader(url, 'PUT');
  return new Promise((resolve, reject) => {
    wx.request({
      url: serverUrl + url,
      data: data,
      header: header,
      method: 'PUT',
      success: (res => {
        if (res.statusCode === 200) {
          //200: 服务端业务处理正常结束
          resolve(res)
        } else {
          reject(res)
        }
      }),
      fail: (res => {
        reject(res)
      })
    })
  })
}
//delete请求
function deleteRequest(url, data) {
  let header = CreateHeader(url, 'DELETE');
  return new Promise((resolve, reject) => {
    wx.request({
      url: serverUrl + url,
      data: data,
      header: header,
      method: 'DELETE',
      success: (res => {
        if (res.statusCode === 200) {
          //200: 服务端业务处理正常结束
          resolve(res)
        } else {
          reject(res)
        }
      }),
      fail: (res => {
        reject(res)
      })
    })
  })
}
module.exports.getRequest = getRequest;
module.exports.uploadFile =uploadFile;
module.exports.postRequest = postRequest;
module.exports.postParamsRequest = postParamsRequest;
module.exports.putRequest = putRequest;
module.exports.deleteRequest = deleteRequest;